7368. Arithmetic mean for figure skaters

 

In a figure skating competition, n judges assign scores to the athletes. A technical official removes all maximum and minimum scores, and the average of the remaining scores is calculated. This result is considered the athlete's final score. Determine the final score for each athlete.

 

Input. The first line contains two integers: the number of judges n (0 < n ≤ 10) and the number of athletes m (0 < m ≤ 100). The next m lines contain n integers each – the scores given by all judges for each athlete.

 

Output. Print m numbers in a single line, each rounded to two decimal places, representing the final score of each athlete.

 

Sample input

Sample output

5 4

7 8 9 8 10

6 5 5 4 7

9 9 10 7 7

7 7 10 9 8

8.33 5.33 9.00 8.50

 

 

SOLUTION

arrays

 

Algorithm analysis

For each athlete, find the minimum min and maximum max scores. Then, compute the average score excluding min and max.

 

Example

For the first skater, the minimum score is 7, and the maximum score is 10. The average of the scores excluding 7 and 10 is (8 + 9 + 8) / 3 = 8.33.

For the last skater, the minimum score is 7, and the maximum score is 10. The average of the scores excluding 7 and 10 is (8 + 9) / 2 = 8.50.

 

Algorithm implementation

Read the number of judges n and the number of athletes m.

 

scanf("%d %d", &n, &m);

 

Process the data for each skater sequentially.

 

for (i = 0; i < m; i++)

{

  scanf("%d", &mas[0]);

 

Find the smallest min and largest max scores for the current i-th skater.

 

  min = max = mas[0];

  for (j = 1; j < n; j++)

  {

    scanf("%d", &mas[j]);

    if (mas[j] > max) max = mas[j];

    if (mas[j] < min) min = mas[j];

  }

 

Compute:

·        The sum of the remaining scores in the variable sum.

·        The count of the remaining scores in the variable cnt.

 

  sum = cnt = 0;

  for (j = 0; j < n; j++)

 

The smallest and largest scores are excluded from the computations.

 

    if ((mas[j] != min) && (mas[j] != max))

    {

      sum += mas[j];

      cnt++;

    }

 

Compute and print the average score for the i-th skater.

 

  res = 1.0 * sum / cnt;

  printf("%.2lf ", res);

}

printf("\n");

 

Python implementation

Read the number of judges n and the number of athletes m.

 

n, m = map(int,input().split())

 

Process the data for each skater sequentially.

 

for _ in range(m):

  lst = list(map(int,input().split()))

 

Find the smallest min and largest max scores for the current skater.

 

  min_el = min(lst)

  max_el = max(lst)

 

Compute:

·        The sum of the remaining scores in the variable s.

·        The count of the remaining scores in the variable cnt.

 

  s = sum(x for x in lst if x != min_el and x != max_el)

  cnt = sum(1 for x in lst if x != min_el and x != max_el)

 

Compute and print the average score for the current skater.

 

  print("%.2f" %(s / cnt), end = ' ')